#include <iostream>
#include "convertor.h"
#include "cdb_to_bdf_convertor.h"
#include "cdb_to_inp_convertor.h"
#include "cae_convertor.h"

CaeConvertor::CaeConvertor() {
}

CaeConvertor::~CaeConvertor() {
}

int CaeConvertor::convert(std::string &inputFilePath, std::string outputFilePath) {
    ModelType inputModleType = determineFileType(inputFilePath);
    ModelType outputModleType = determineFileType(outputFilePath);
    if (inputModleType == MT_UNKNOWN)
    {
        std::cerr << "Unsupported file: " << inputFilePath << std::endl;
        return -1;
    }
    if (outputModleType == MT_UNKNOWN)
    {
        std::cerr << "Unsupported file: " << outputFilePath << std::endl;
        return -1;
    }

    Convertor *convertor = Convertor::create(inputModleType, outputModleType);
    if (convertor != nullptr) {
        convertor->convert(inputFilePath, outputFilePath);
	delete convertor;
    }

    return 0;
}

CaeConvertor::ModelType CaeConvertor::determineFileType(std::string &filePath) {
    size_t dotPos = filePath.find_last_of('.');
    if (dotPos != std::string::npos &&dotPos > 0)
    {
        std::string extension = filePath.substr(dotPos);
        if (extension == ".inp")
        {
            return MT_INP;
        }
        else if (extension == ".cdb")
        {
            return MT_CDB;
        }
        else if (extension == ".bdf")
        {
            return MT_BDF;
        }
        else
        {
            return MT_UNKNOWN;
        }
    }
    else
    {
        return MT_UNKNOWN;
    }
}

